<?php

// +----------------------------------------------------------------------
// | MyCentOS
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.mycentos.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 琥珀 <317559272@qqq.com><http://www.mycentos.com>
// +----------------------------------------------------------------------
// | @date  : 2015-03-05 14:13:54
// +----------------------------------------------------------------------
// | @Last Modified by:  琥珀
// +----------------------------------------------------------------------
// | @Last Modified time: 2015-03-14 14:54:20
// +-----------------------------------------------------------------------


/**
 * 登陆控制器
 * @authors 琥珀 (317559272@qq.com)
 */
namespace Admin\Controller;
use Think\Controller;
Class PublicController extends Controller
{
    public function index() {
        if (is_login()) {
            $this->redirect('Index/index');
        } else {
            $this->redirect('Public/login');
        }
    }
    public function login($username = null, $password = null, $verify = null) {
        if (IS_POST) {

            // if (!check_verify($verify)) {
            //     $this->error('验证码输入错误！');
            // }
            $username = I("username");
            $password = I("password");
            $Login = D('Login');
            $rs = $Login->login($username, $password);
            if (0 < $rs) {
                $this->redirect('Index/index');
            } else {

                //登录失败
                switch ($rs) {
                    case -1:
                        $error = '用户不能为空!';
                        break;

                    case -2:
                        $error = '密码不能为空！';
                        break;

                    case -3:
                        $error = '用户不存在或被禁用！';
                        break;

                    case -4:
                        $error = '密码错误';
                        break;

                    default:
                        $error = '未知错误！';
                        break;
                }
                $this->error($error);
            }
        } else {
            if (is_login()) {
                $this->redirect('Index/index');
            } else {
                $this->display();
            }
        }
    }
    /**
     * 系统用到参数  ajax赋值扩展
     * @access protected
     * @param array $result  引用传值
     * @return void
     */
    protected function ajaxAssign(&$result) {
        $result['statusCode'] = $result['status'];
        $result['message'] = $result['info'];
        $result['navTabId'] = $_REQUEST['navTabId'];
        $result['rel'] = $_REQUEST['rel'];
        $result['callbackType'] = $_REQUEST['callbackType'];
        $result['forwardUrl'] = $_REQUEST['forwardUrl'];
        $result['confirmMsg'] = $_REQUEST['confirmMsg'];
    }

    /**
     * 系统用到参数  ajax赋值扩展 ,继承Controller的ajaxReturn
     * @access protected
     * @param array $data,$type  引用传值
     * @return void
     */
    protected function ajaxReturn($data, $type = '') {
        if (method_exists($this, "ajaxAssign")) $this->ajaxAssign($data);
        parent::ajaxReturn($data, $type);

        //调用Thinkphp的ajaxReturn

    }

    //退出登录
    public function logout() {
        if (is_login()) {
            D('Login')->logout();
            session('[destroy]');
            $this->redirect('Public/login');
        } else {
            $this->error('退出失败');
        }
    }

    //获取验证码
    public function verify() {
        $verify = new \Think\Verify();
        $verify->entry(1);
    }
    public function delcache() {

        //清文件缓存
        $dirs = array('./Runtime/');

        //清理缓存
        foreach ($dirs as $value) {
            if ($this->rmdirr($value)) {
                $data['info'].= '文件夹' . $value . '删除成功;';
                @mkdir($value, 0777, true);
            }
        }
        $data['status'] = 1;
        $data['callbackType'] = 'forward';
        $this->ajaxReturn($data);
    }
    public function rmdirr($dirname) {
        if (!file_exists($dirname)) {
            return false;
        }
        if (is_file($dirname) || is_link($dirname)) {
            return unlink($dirname);
        }
        $dir = dir($dirname);
        if ($dir) {
            while (false !== $entry = $dir->read()) {
                if ($entry == '.' || $entry == '..') {
                    continue;
                }
                $this->rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);
            }
        }
        $dir->close();
        return rmdir($dirname);
    }
}
?>